package net.dgg.rz.evaluating.service;

import net.dgg.framework.tac.utils.DggValidateUtil;
import net.dgg.framework.tac.utils.idutil.DggKeyWorker;
import net.dgg.rz.common.base.service.BaseService;
import net.dgg.rz.common.excption.CommonExcption;
import net.dgg.rz.evaluating.dao.*;
import net.dgg.rz.evaluating.entity.*;
import net.dgg.rz.evaluating.entity.dto.*;
import net.dgg.rz.production.exception.ProductOrderExcption;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zyou on 2019/5/22.
 */
@Service
public class EvaluatingService extends BaseService {
    @Autowired
    private EvaluatingBaseChannelDao evaluatingBaseChannelDao;
    @Autowired
    private EvaluatingBasicOptionsDao evaluatingBasicOptionsDao;
    @Autowired
    private EvaluatingTestPaperDao evaluatingTestPaperDao;
    @Autowired
    private EvaluatingOptionsChannelDao evaluatingOptionsChannelDao;
    @Autowired
    private EvaluatingPaperSubjectDao evaluatingPaperSubjectDao;
    @Autowired
    private EvaluatingSubjectOptionsDao evaluatingSubjectOptionsDao;
    @Autowired
    private EvaluatingChannelFieldDao evaluatingChannelFieldDao;
    @Autowired
    private EvaluatingAssessmentResultsDao evaluatingAssessmentResultsDao;
    @Autowired
    private SessionManager sessionManager;
    /**
     * 渠道列表
     * @param params
     * @return
     */
    public List<EvaluatingBaseChannel> channelListWithPage(Map params) {
        return evaluatingBaseChannelDao.channelListWithPage(params);
    }

    /**
     * 根据id查询渠道
     * @param id
     * @return
     */
    public EvaluatingBaseChannel getBaseChannelById(Long id) {
        return evaluatingBaseChannelDao.selectById(id);
    }

    /**
     * 判断名字，code是否重复
     * @param channelNameMap
     * @return
     */
    public int countByParams(Map channelNameMap) {
        return evaluatingBaseChannelDao.countByParams(channelNameMap);
    }

    /**
     * 保存渠道
     * @param evaluatingBaseChannel
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveChannel(EvaluatingBaseChannel evaluatingBaseChannel) {
        UserEntity userEntity = sessionManager.getCurrentUser();
        if(evaluatingBaseChannel.getId()==null){
            evaluatingBaseChannel.setId(DggKeyWorker.nextId());
            evaluatingBaseChannel.setFlag(0);
            saveOpt(evaluatingBaseChannel,userEntity);
            updateOpt(evaluatingBaseChannel,userEntity);
            evaluatingBaseChannelDao.insertAllColumn(evaluatingBaseChannel);
        }else {
            updateOpt(evaluatingBaseChannel,userEntity);
            evaluatingBaseChannelDao.updateById(evaluatingBaseChannel);
        }
    }

    /**
     * 题目列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> subjectListWithPage(Map params) {
        return evaluatingBasicOptionsDao.subjectListWithPage(params);
    }

    /**
     * 选项列表
     * @param params
     * @return
     */
    public List<EvaluatingBasicOptions> optionsListWithPage(Map params) {
        return evaluatingBasicOptionsDao.optionsListWithPage(params);
    }

    /**
     * 评估结果列表
     * @param params
     * @return
     */
    public List<EvaluatingResults> assessmentListWithPage(Map params) {
        return evaluatingAssessmentResultsDao.assessmentListWithPage(params);
    }
    /**
     * 根据id查询选项
     * @param id
     * @return
     */
    public EvaluatingBasicOptions getBaseOptionsById(Long id) {
        return evaluatingBasicOptionsDao.selectById(id);
    }

    /**
     * 根据id查询题目
     * @param id
     * @return
     */
    public EvaluatingPaperSubject getBaseSubjectById(Long id) {
        return evaluatingPaperSubjectDao.selectById(id);
    }

    /**
     * 根据id查询评估结果级别
     * @param id
     * @return
     */
    public EvaluatingRatingLevel getRatingLevelById(Long id) {
        return evaluatingAssessmentResultsDao.selectRatingLevelById(id);
    }

    /**
     * 根据id查询评估结果
     * @param id
     * @return
     */
    public EvaluatingResults getAssessmentResultsById(Long id) {
        return evaluatingAssessmentResultsDao.selectById(id);
    }
    /**
     * 选项名称/code判重
     * @param optionsNameMap
     * @return
     */
    public int countOptionsByParams(Map optionsNameMap) {
        return evaluatingBasicOptionsDao.countOptionsByParams(optionsNameMap);
    }

    /**
     * 评估结果名称判重
     * @param optionsNameMap
     * @return
     */
    public int countAssessmentByParams(Map optionsNameMap) {
        return evaluatingAssessmentResultsDao.countAssessmentByParams(optionsNameMap);
    }

    /**
     * 保存选项
     * @param evaluatingBasicOptions
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveOptions(EvaluatingBasicOptions evaluatingBasicOptions) {
        UserEntity userEntity = sessionManager.getCurrentUser();
        if(evaluatingBasicOptions.getId()==null){
            evaluatingBasicOptions.setId(DggKeyWorker.nextId());
            evaluatingBasicOptions.setFlag(0);
            saveOpt(evaluatingBasicOptions,userEntity);
            updateOpt(evaluatingBasicOptions,userEntity);
            evaluatingBasicOptionsDao.insertAllColumn(evaluatingBasicOptions);
        }else {
            updateOpt(evaluatingBasicOptions,userEntity);
            evaluatingBasicOptionsDao.updateById(evaluatingBasicOptions);
        }
    }

    /**
     * 保存评估结果
     * @param evaluatingResults
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveAssessment(EvaluatingResults evaluatingResults) {
        UserEntity userEntity = sessionManager.getCurrentUser();
        EvaluatingTestPaper evaluatingTestPaper = evaluatingTestPaperDao.selectById(evaluatingResults.getPaperId());
        List<EvaluatingResults> list = null;
        Map params = new HashMap();
        params.put("paperId",evaluatingTestPaper.getId());
        params.put("id",evaluatingResults.getId());
        list = evaluatingAssessmentResultsDao.findResultssList(params);
        if(!CollectionUtils.isEmpty(list)){
            throw new ProductOrderExcption("该试卷已被绑定！！！");
        }
        if(evaluatingResults.getId()==null){
            evaluatingResults.setId(DggKeyWorker.nextId());
            evaluatingResults.setIsMatching(1);//是否绑定试卷   已绑定
            evaluatingResults.setState(1);//状态   1 正常
            evaluatingResults.setPaperName(evaluatingTestPaper.getPaperName());
            evaluatingResults.setPaperCode(evaluatingTestPaper.getPaperCode());
            saveOpt(evaluatingResults,userEntity);
            updateOpt(evaluatingResults,userEntity);
            evaluatingAssessmentResultsDao.insertAllColumn(evaluatingResults);
        }else {
            evaluatingResults.setPaperName(evaluatingTestPaper.getPaperName());
            evaluatingResults.setPaperCode(evaluatingTestPaper.getPaperCode());
            updateOpt(evaluatingResults,userEntity);
            evaluatingAssessmentResultsDao.updateById(evaluatingResults);
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public EvaluatingTestPaper getPaperById(Long id) {
        return evaluatingTestPaperDao.selectById(id);
    }
    /**
     * 保存试题
     * @param evaluatingTestPaper
     */
    @Transactional(rollbackFor = Exception.class)
    public void savePaper(EvaluatingTestPaper evaluatingTestPaper) {
        UserEntity userEntity = sessionManager.getCurrentUser();
        if(evaluatingTestPaper.getId()==null){
            evaluatingTestPaper.setId(DggKeyWorker.nextId());
            evaluatingTestPaper.setFlag(0);
            saveOpt(evaluatingTestPaper,userEntity);
            updateOpt(evaluatingTestPaper,userEntity);
            evaluatingTestPaperDao.insertAllColumn(evaluatingTestPaper);
        }else {
            updateOpt(evaluatingTestPaper,userEntity);
            evaluatingTestPaperDao.updateById(evaluatingTestPaper);
        }
    }

    /**
     * 试题name/code去重
     * @param paperNameMap
     * @return
     */
    public int countPaperByParams(Map paperNameMap) {
        return evaluatingTestPaperDao.countPaperByParams(paperNameMap);
    }

    /**
     * 试题列表
     * @return
     */
    public List<EvaluatingTestPaper> paperList() {
        return evaluatingTestPaperDao.paperList();
    }
    /**
     * 试题列表
     * @param params
     * @return
     */
    public List<EvaluatingTestPaper> paperListWithPage(Map params) {
        return evaluatingTestPaperDao.paperListWithPage(params);
    }

    /**
     * 查询选项所关联的渠道
     * @param optionsId
     * @return
     */
    public List<EvaluatingOptionsChannel> findChannelByOptionsId(Long optionsId) {
        return evaluatingOptionsChannelDao.findChannelByOptionsId(optionsId);
    }
    /**
     * 查询选项所关联的渠道
     * @param optionsId
     * @return
     */
    public List<Long> findChannelIdsOptionsId(Long optionsId) {
        return evaluatingOptionsChannelDao.findChannelIdsOptionsId(optionsId);
    }

    /**
     *保存 选项关联渠道
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveOptionsChannel(EvaluatingOptionsChannelDto evaluatingOptionsChannelDto) {
        //首先删除所有关联渠道
        Long optionsId = evaluatingOptionsChannelDto.getOptionsId();
        Map params = new HashMap();
        params.put("optionsId",optionsId);
        evaluatingOptionsChannelDao.deleteByMap(params);
        //保存关联渠道
        List<EvaluatingOptionsChannel> evaluatingOptionsChannels = evaluatingOptionsChannelDto.getEvaluatingOptionsChannels();
        if(evaluatingOptionsChannels != null && evaluatingOptionsChannels.size()>0){
            for (EvaluatingOptionsChannel evaluatingOptionsChannel:evaluatingOptionsChannels){
                evaluatingOptionsChannel.setId(DggKeyWorker.nextId());
                evaluatingOptionsChannelDao.insertAllColumn(evaluatingOptionsChannel);
            }
        }
    }
    /**
     *保存 试题题目
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void savePaperSubject(EvaluatingPaperSubjectDto evaluatingPaperSubjectDto) {
        UserEntity userEntity = sessionManager.getCurrentUser();
        Long paperId = evaluatingPaperSubjectDto.getPaperId();
        //通过试题id删除关联题目
        Map params = new HashMap();
        params.put("paperId",paperId);
        evaluatingPaperSubjectDao.deleteByMap(params);
        //通过试题id删除关联选项
        evaluatingSubjectOptionsDao.deleteByMap(params);
        //保存试题关联题目
        List<EvaluatingSubjectOptionsDto> evaluatingPaperSubjectDtos = evaluatingPaperSubjectDto.getEvaluatingPaperSubjectDtos();
        for (EvaluatingSubjectOptionsDto evaluatingSubjectOptionsDto:evaluatingPaperSubjectDtos){
            //通过选择题目的方式获取，不再需要重新新增题目,做编辑操作
            EvaluatingPaperSubject evaluatingPaperSubject = evaluatingPaperSubjectDao.selectById(evaluatingSubjectOptionsDto.getId());
            evaluatingPaperSubject.setPaperId(paperId);
            evaluatingPaperSubject.setSubjectSort(evaluatingSubjectOptionsDto.getSubjectSort());
            evaluatingPaperSubject.setSubjectType(evaluatingSubjectOptionsDto.getSubjectType());
            this.updateOpt(evaluatingPaperSubject,userEntity);
            evaluatingPaperSubjectDao.updateById(evaluatingPaperSubject);
            //题目关联选项
            List<EvaluatingSubjectOptions> evaluatingSubjectOptionss = evaluatingSubjectOptionsDto.getEvaluatingSubjectOptionss();
            //保存题目关联选项
            int i = 1;
            for (EvaluatingSubjectOptions evaluatingSubjectOptions:evaluatingSubjectOptionss){
                evaluatingSubjectOptions.setId(DggKeyWorker.nextId());
                evaluatingSubjectOptions.setPaperId(paperId);
                evaluatingSubjectOptions.setSubjectId(evaluatingSubjectOptionsDto.getId());
                evaluatingSubjectOptions.setSubjectOptionsSort(i++);
                evaluatingSubjectOptionsDao.insertAllColumn(evaluatingSubjectOptions);
            }
        }
    }

    /**
     * 根据试题id查询题目
     * @param params
     * @return
     */
    public List<SubjectOptionsDto> queryPaperSubjectByPaperId(Map params) {
        DggValidateUtil.notNull(params, CommonExcption.class,"对象为空");
        DggValidateUtil.notNull(params.get("paperId"), CommonExcption.class,"id为空");
        return evaluatingPaperSubjectDao.queryPaperSubjectByPaperId(params);
    }

    /**
     * 查询所有题目
     * @return
     */
    public List<SubjectOptionsDto> queryPaperSubject() {
        return evaluatingPaperSubjectDao.queryPaperSubject();
    }

    /**
     * 保存题目
     * @param evaluatingPaperSubject
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveSubject(EvaluatingPaperSubject evaluatingPaperSubject) {
        UserEntity userEntity = sessionManager.getCurrentUser();
        if(evaluatingPaperSubject.getId()==null){
            evaluatingPaperSubject.setId(DggKeyWorker.nextId());
            evaluatingPaperSubject.setFlag(0);
            saveOpt(evaluatingPaperSubject,userEntity);
            updateOpt(evaluatingPaperSubject,userEntity);
            evaluatingPaperSubjectDao.insertAllColumn(evaluatingPaperSubject);
        }else {
            updateOpt(evaluatingPaperSubject,userEntity);
            evaluatingPaperSubjectDao.updateById(evaluatingPaperSubject);
        }
    }

    /**
     * 查询所有评估结果级别
     * @return
     */
    public List<EvaluatingRatingLevel> queryAssessmentLevelWithPage(Map params) {
        return evaluatingAssessmentResultsDao.queryAssessmentLevelWithPage(params);
    }

    /**
     * 根据结果id查询客户级别
     * @param resultsId
     * @param isMath
     * @return
     */
    public List<EvaluatingRatingLevel> queryRatingLevelByAssessmentId(Long resultsId,Integer isMath) {
        DggValidateUtil.notNull(resultsId, CommonExcption.class,"resultsId为空");
        DggValidateUtil.notNull(isMath, CommonExcption.class,"类型为空");
        Map params = new HashMap();
        params.put("resultsId",resultsId);
        params.put("isMath", isMath);
        return evaluatingAssessmentResultsDao.queryRatingLevelByAssessmentId(params);
    }

    /**
     * 根据题目id查询选项
     * @param subjectId
     * @param isMath
     * @return
     */
    public List<EvaluatingBasicOptions> queryOptionsBySubjectId(Long subjectId,Integer isMath) {
        DggValidateUtil.notNull(subjectId, CommonExcption.class,"subjectId为空");
        DggValidateUtil.notNull(isMath, CommonExcption.class,"类型为空");
        Map params = new HashMap();
        params.put("subjectId",subjectId);
        params.put("isMath", isMath);
        return evaluatingBasicOptionsDao.queryOptionsBySubjectId(params);
    }

    /**
     * 编辑客户级别绑定
     * @param resultsId
     * @param levelId
     * @param handleType
     * @return
     */
    public void editRatingLevelBinding(Long resultsId, Long levelId,Integer handleType) {
//        UserEntity userEntity = sessionManager.getCurrentUser();
        Map params = new HashMap();
        params.put("id",levelId);
        EvaluatingRatingLevel evaluatingRatingLevel = evaluatingAssessmentResultsDao.queryRatingLevelById(params);
        if(handleType == 1){
            evaluatingRatingLevel.setResultsId(null);
            evaluatingRatingLevel.setResultsName(null);
        }else{
            EvaluatingResults evaluatingResults = evaluatingAssessmentResultsDao.selectById(resultsId);
            evaluatingRatingLevel.setResultsId(evaluatingResults.getId());
            evaluatingRatingLevel.setResultsName(evaluatingResults.getResultsName());
        }
//        this.updateOpt(evaluatingRatingLevel,userEntity);
        evaluatingAssessmentResultsDao.updateRatingLevel(evaluatingRatingLevel);
    }

    /**
     * 编辑选项绑定
     * @param subjectId
     * @param optionId
     * @param handleType
     * @return
     */
    public void editOptionBinding(Long subjectId, Long optionId,Integer handleType) {
//        UserEntity userEntity = sessionManager.getCurrentUser();
        EvaluatingBasicOptions evaluatingBasicOptions = evaluatingBasicOptionsDao.selectById(optionId);
        if(handleType == 1){
            evaluatingBasicOptions.setSubjectId(null);
            evaluatingBasicOptions.setSubjectName(null);
        }else{
            EvaluatingPaperSubject evaluatingPaperSubject = evaluatingPaperSubjectDao.selectById(subjectId);
            evaluatingBasicOptions.setSubjectId(evaluatingPaperSubject.getId());
            evaluatingBasicOptions.setSubjectName(evaluatingPaperSubject.getSubjectName());
        }
//        this.updateOpt(evaluatingRatingLevel,userEntity);
        evaluatingBasicOptionsDao.updateOptions(evaluatingBasicOptions);
    }

    /**
     *保存 渠道字段
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveChannelField(EvaluatingChannelFieldDto evaluatingChannelFieldDto) {
        UserEntity userEntity = sessionManager.getCurrentUser();
        //通过渠道id删除关联字段
        Map params = new HashMap();
        params.put("channelId",evaluatingChannelFieldDto.getChannelId());
        params.put("fieldType",evaluatingChannelFieldDto.getFieldType());
        evaluatingChannelFieldDao.deleteByMap(params);
        //保存渠道字段
        List<EvaluatingChannelField> evaluatingChannelFields = evaluatingChannelFieldDto.getEvaluatingChannelFields();
        for(EvaluatingChannelField evaluatingChannelField:evaluatingChannelFields){
            evaluatingChannelField.setId(DggKeyWorker.nextId());
            evaluatingChannelField.setFlag(0);
            this.saveOpt(evaluatingChannelField,userEntity);
            this.updateOpt(evaluatingChannelField,userEntity);
            evaluatingChannelFieldDao.insertAllColumn(evaluatingChannelField);
        }
    }

    /**
     * 根据渠道id查询题目
     * @param params
     * @return
     */
    public List<EvaluatingChannelField> queryChannelFieldByChannelId( Map params) {
        return evaluatingChannelFieldDao.selectByMap(params);
    }
    /**
     * <p>
     * 根据 entity 条件，查询一条记录
     * </p>
     * @return
     */
    public EvaluatingTestPaper selectOne(EvaluatingTestPaper evaluatingTestPaper) {
        return evaluatingTestPaperDao.selectOne(evaluatingTestPaper);
    }
    /**
     * <p>
     * 根据 entity 条件，查询多条记录
     * </p>
     * @return
     */
    public List<EvaluatingTestPaper> selectAll(EvaluatingTestPaper evaluatingTestPaper) {
        return evaluatingTestPaperDao.selectAll(evaluatingTestPaper);
    }

    /**
     * 根据分数查询所匹配的等级等内容
     * @param params
     * @return
     */
    public Map selectLevel( Map params) {
        return evaluatingTestPaperDao.selectLevel(params);
    }

    /**
     * 查询渠道信息
     * @param redisChannelIds
     * @return
     */
    public List<EvaluatingBaseChannel> findChannelBatchIds(List redisChannelIds) {
        return evaluatingBaseChannelDao.selectBatchIds(redisChannelIds);
    }


    /**
     * 查询评估结果
     * @param paperId
     * @return
     */
    public EvaluatingResults findResultsBypaperId(Long paperId) {
        return evaluatingAssessmentResultsDao.findResultsBypaperId(paperId);
    }

    /**
     * 保存评估结果级别
     * @param evaluatingRatingLevel
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveRatingLevel(EvaluatingRatingLevel evaluatingRatingLevel) {
        UserEntity userEntity = sessionManager.getCurrentUser();
        if(evaluatingRatingLevel.getId()==null){
            evaluatingRatingLevel.setId(DggKeyWorker.nextId());
            evaluatingRatingLevel.setFlag(0);
            saveOpt(evaluatingRatingLevel,userEntity);
            updateOpt(evaluatingRatingLevel,userEntity);
            evaluatingAssessmentResultsDao.insertRatingLevelAllColumn(evaluatingRatingLevel);
        }else {
            updateOpt(evaluatingRatingLevel,userEntity);
            evaluatingAssessmentResultsDao.updateRatingLevelById(evaluatingRatingLevel);
        }
    }

    /**
     * 导入试题
     * @param mapList
     */
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(List<Map> mapList) {
        UserEntity userEntity = sessionManager.getCurrentUser();
        if(CollectionUtils.isEmpty(mapList)){
            throw new ProductOrderExcption("未解析到EXCEL的数据");
        }else{
            //循环试题
            for(Map map:mapList){
                //保存试题
                DggValidateUtil.notNull(map.get("paperName"),ProductOrderExcption.class,"试题名称为空");
                DggValidateUtil.notNull(map.get("areaCode"),ProductOrderExcption.class,"地域CODE为空");
                EvaluatingTestPaper evaluatingTestPaper = new EvaluatingTestPaper();
                Long paperId = DggKeyWorker.nextId();
                evaluatingTestPaper.setId(paperId);
                evaluatingTestPaper.setPaperName(map.get("paperName").toString());
                evaluatingTestPaper.setPaperAreaCode(map.get("areaCode").toString());
                evaluatingTestPaper.setPaperCategory(1);
                evaluatingTestPaper.setFlag(0);
                evaluatingTestPaper.setPaperType(1);
                evaluatingTestPaper.setPaperScore(60D);
                evaluatingTestPaper.setPaperCode(map.get("areaCode")+"_"+map.get("paperName"));
                //判断name重复
                Map paperNameMap = new HashMap();
                paperNameMap.put("paperName",evaluatingTestPaper.getPaperName());
                paperNameMap.put("id",evaluatingTestPaper.getId());
                int countPaperName = countPaperByParams(paperNameMap);
                if(countPaperName>0){
                    throw new ProductOrderExcption("试题名称重复");
                }
                //判断code重复
                Map paperCodeMap = new HashMap();
                paperCodeMap.put("paperCode",evaluatingTestPaper.getPaperCode());
                paperCodeMap.put("id",evaluatingTestPaper.getId());
                int countPaperCode = countPaperByParams(paperCodeMap);
                if(countPaperCode>0){
                    throw new ProductOrderExcption("试题编号重复");
                }
                saveOpt(evaluatingTestPaper,userEntity);
                updateOpt(evaluatingTestPaper,userEntity);
                evaluatingTestPaperDao.insertAllColumn(evaluatingTestPaper);
                List<Map> subjectList = (List<Map>)map.get("subjects");
                if(CollectionUtils.isEmpty(subjectList)){
                    throw new ProductOrderExcption("未解析到题目");
                }else{
                    //循环题目列表
                    for(int i = 0;i<subjectList.size() ;i++){
                        //保存题目
                        DggValidateUtil.notNull(subjectList.get(i).get("subjectName"),ProductOrderExcption.class,"题目名称为空");
                        EvaluatingPaperSubject evaluatingPaperSubject = new EvaluatingPaperSubject();
                        Long subjectId = DggKeyWorker.nextId();
                        String subjectName = subjectList.get(i).get("subjectName").toString();
                        evaluatingPaperSubject.setId(subjectId);
                        evaluatingPaperSubject.setPaperId(paperId);
                        evaluatingPaperSubject.setSubjectSort(i+1);
                        evaluatingPaperSubject.setSubjectName(subjectName);
                        evaluatingPaperSubject.setFlag(0);
                        saveOpt(evaluatingPaperSubject,userEntity);
                        updateOpt(evaluatingPaperSubject,userEntity);
                        evaluatingPaperSubjectDao.insertAllColumn(evaluatingPaperSubject);
                        List<Map> optionList = (List<Map>)subjectList.get(i).get("options");
                        if(CollectionUtils.isEmpty(optionList)){
                            throw new ProductOrderExcption("未解析到选项");
                        }else{
                            int j = 1;
                            //循环选项列表
                            for(Map optionMap : optionList){
                                //保存选项
                                DggValidateUtil.notNull(optionMap.get("optionName"),ProductOrderExcption.class,"选项名称为空");
                                DggValidateUtil.notNull(optionMap.get("score"),ProductOrderExcption.class,"选项分数为空");
                                EvaluatingBasicOptions evaluatingBasicOptions = new EvaluatingBasicOptions();
                                Long optionId = DggKeyWorker.nextId();
                                evaluatingBasicOptions.setId(optionId);
                                evaluatingBasicOptions.setOptionsCode(optionMap.get("optionName").toString());
                                evaluatingBasicOptions.setOptionsName(optionMap.get("optionName").toString());
                                evaluatingBasicOptions.setSubjectId(subjectId);
                                evaluatingBasicOptions.setSubjectName(subjectName);
                                if(optionMap.get("score").toString().contains("-")){
                                    evaluatingBasicOptions.setOptionsScoreType(2);
                                    evaluatingBasicOptions.setOptionsScore(Double.valueOf(optionMap.get("score").toString().substring(1,optionMap.get("score").toString().length()-1)));
                                }else{
                                    evaluatingBasicOptions.setOptionsScoreType(1);
                                    evaluatingBasicOptions.setOptionsScore(Double.valueOf(optionMap.get("score").toString()));
                                }
                                evaluatingBasicOptions.setFlag(0);
                                saveOpt(evaluatingBasicOptions,userEntity);
                                updateOpt(evaluatingBasicOptions,userEntity);
                                evaluatingBasicOptionsDao.insertAllColumn(evaluatingBasicOptions);
                                //保存试题，题目,选项关系数据
                                EvaluatingSubjectOptions evaluatingSubjectOptions = new EvaluatingSubjectOptions();
                                evaluatingSubjectOptions.setId(DggKeyWorker.nextId());
                                evaluatingSubjectOptions.setPaperId(paperId);
                                evaluatingSubjectOptions.setSubjectId(subjectId);
                                evaluatingSubjectOptions.setOptionsId(optionId);
                                evaluatingSubjectOptions.setSubjectOptionsSort(j++);
                                evaluatingSubjectOptionsDao.insertAllColumn(evaluatingSubjectOptions);
                            }
                        }
                    }
                }
            }
        }
    }

}
